import React, { Component, PropTypes } from 'react'

import './../css/default-select.css'

class SelectInput extends Component {

    constructor(props, context) {
        super(props, context)

        this.state = {val: this.props.val ? this.props.val : ""}; // 初始化状态
    }

    componentWillReceiveProps(nextProps){//初始化状态
        if(nextProps.val){
            this.setState({
                val : nextProps.val
            })
        }
    }

    handleChange(e) {
        var val = e.target.value;
        this.setState({val: val});
        this.props.onChange(val)
    }

    render() {
        const {id, selectClass, optionsData} = this.props;
        var val = this.state.val;
        var reactId = 0;
        return (
            <select id={id} className={selectClass ? selectClass : 'default-select'} value={val} disabled={this.props.disabled?true:false}
                    onChange={this.handleChange.bind(this)}>
                {optionsData}
            </select>
        )
    }
}

export default SelectInput
